package ru.ifmo.ctddev.krotkov.helloudp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;

public class Task implements Runnable {
	DatagramSocket socket;
	DatagramPacket pack;

	public void run() {
		Scanner in = new Scanner(new ByteArrayInputStream(pack.getData()));
		String s = in.nextLine();
		s = "Hello, " + s;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintWriter out = new PrintWriter(baos);
		out.println(s);
		out.flush();
		DatagramPacket ans = new DatagramPacket(baos.toByteArray(), baos.size());
		ans.setAddress(pack.getAddress());
		ans.setPort(pack.getPort());
		try {
			socket.send(ans);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public Task(DatagramSocket socket, DatagramPacket pack) {
		super();
		this.socket = socket;
		this.pack = pack;
	}

}
